package com.teampower.idea.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.teampower.idea.dao.TestUsersDAO;
import com.teampower.idea.pojo.TestUsers;

public class TestUsersDAOImpl extends HibernateDaoSupport implements TestUsersDAO {
	
	public TestUsers loadOneUser(String username,String password) {
		String hql = "from TestUsers as t where t.username=? and t.password=?";
		List list = null;
		list = this.getHibernateTemplate().find(hql,new Object[]{username,password});
		if(list.size()==1){
			return (TestUsers) list.get(0);
		}else{
			return null;
		}
	}

	//分页查询
	public List listData(int startIndex,int pageSize) {
		Session session = SessionFactoryUtils.getSession(this.getHibernateTemplate().getSessionFactory(),true);
		Query query = session.createQuery("from TestUsers as t where permission!='9' order by username");
		query.setFirstResult(startIndex-1);
		query.setMaxResults(pageSize);
		List list = query.list();
		SessionFactoryUtils.releaseSession(session,this.getHibernateTemplate().getSessionFactory());
		return list;
	}
	
	//加载单条数据
	public TestUsers loadOneUser(String id) {
		String hql = "from TestUsers as t where t.id=?";
		List list = null;
		list = this.getHibernateTemplate().find(hql,id);
		if(list.size()==1){
			return (TestUsers) list.get(0);
		}else{
			return null;
		}
	}

	//添加单条数据
	public void save(TestUsers users) {
		this.getHibernateTemplate().save(users);
	}
	//删除单条数据
	public void delete(TestUsers users) {
		this.getHibernateTemplate().delete(users);
	}
	//更新单条数据
	public void updateOneUser(TestUsers users) {
		this.getHibernateTemplate().update(users);
	}
	
	
}

